/* This version of Pedro requires mootools.v1.00 or higher */

var d = document;

var pedro = {};

function register(objStr){
    if(isUndef(eval(objStr))) { eval(objStr + " = {}"); }
}

function require(objStr,pckg){
    if(isUndef(eval(objStr))) throw Error(pckg + " requires " + objStr);
}

var byId = $;

/* Updated to user Moo $type */
function isUndef(obj)  { return ($type(obj) == false); }
function isString(obj) { return ($type(obj) == 'string'); }
function isObject(obj) { return ($type(obj) == 'object'); }
function isFunction(obj) { return ($type(obj) == 'function'); }
function isNumber(obj){ return ($type(obj) == 'number'); }
function isNull(obj) { return (obj == null); }

/* Browser */

register("pedro.browser");

/* Updated to user Moo window */
pedro.browser.agt = navigator.userAgent.toLowerCase();
pedro.browser.isIE = window.ie;
pedro.browser.isIE6 = window.ie6;
pedro.browser.isIE7 = window.ie7;
pedro.browser.isNS = window.gecko;
pedro.browser.isSF = window.khtml;
pedro.browser.version = navigator.appVersion;

/* OS */

register("pedro.os");

pedro.os.isWindows = (navigator.platform.indexOf("Win") != -1);
pedro.os.isLinux = (navigator.platform.indexOf("Linux") != -1);
pedro.os.isMac = (navigator.platform.indexOf("Mac") != -1);

/* Extends */

/* Not going to touch this */
pedro.Base = new Function();

pedro.Base.extend = function(sub){
    sub.prototype = new this;
    sub.prototype._super = this;
    sub.extend = this.extend;
    sub.prototype.constructor = sub;
    return sub;
}

/* Easy Dom */

function createElementFunction(name) {
    var stringType = typeof '';

    return function() {
        var elem = document.createElement(name);

        if (arguments.length == 0) return elem;

        var firstChild = 0;
        if (typeof arguments[0] != 'string'
            && typeof arguments[0].nodeType == 'undefined') {

            var attrs = arguments[0];
            for (var attr in attrs) {
                var attrName = attr;
                if (pedro.browser.isIE) {
                    if (!isUndef(ieAttrNameOverrides[attrName])) {
                        attrName = ieAttrNameOverrides[attrName];
                    }
                }
                elem.setAttribute(attrName, attrs[attr]);
            }
            firstChild = 1;
        }

        for (var i = firstChild; i < arguments.length; i++) {
            var child = arguments[i];
            if (child) { // Skip falsey arguments.
              if (typeof child == stringType) {
                  child = document.createTextNode(child);
              }
              elem.appendChild(child);
            }
        }

        return $(elem); //Gives it all the goodness of mootools
    };
}

var ieAttrNameOverrides = {
    "class" : "className",
    "maxlength" : "maxLength"
};

var tags = [
    "script",
    "p",
    "a",
    "div", "span",
    "ul", "ol", "li",
    "img",
    "hr",
    "br",
    "em", "strong",
    "table", "tbody", "tr", "th", "td",
    "input", "select", "option", "form",
    "iframe"
];

//registers the functions in the page scope
for (var i in tags) {
  window[tags[i]] = createElementFunction(tags[i]);
}
